<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Purchase extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->layout->setLayout('frontend/index');
        $uid = $this->session->userdata('logged_in')['uid'];
        if (!$uid) redirect(base_url());
    }

    public function cart(){
        $uid = $this->session->userdata('logged_in')['uid'];
        $data['products'] = $this->mcart->get($uid);
        $data['user'] = $this->muser->getDiscount($uid);
        if($this->session->userdata('logged_in'))
        {
            $session_data = $this->session->userdata('logged_in');
            $data['is_logged_in'] = true;
            $data['fullname'] = $session_data['fullname'];
        }
        else
        {
            $data['is_logged_in'] = false;
        }
        if (isset($_POST['submit']) and $_POST['submit'] == 'payment'){
            $address = $this->input->post('received_address');
            $contact = $this->input->post('contact_number');
            $time = $this->input->post('time_received');
            $date = $this->input->post('date_received');
            $message = $this->input->post('message');
            $carts = $this->mcart->get($uid);
            $total = $this->input->post('cart_total');
            $invoice_data = array(
                'total' => $total,
                'status' => '0',
                'invoice_date' => gmdate('Y-m-d'),
                'user_id' => $uid,
                'received_address' => $address,
                'contact_number' => $contact,
                'received_time' => $time,
                'received_date' => $date,
                'shipping_note' => $message
            );
            $id = $this->minvoice->createInvoice($invoice_data);

            if($id){
                foreach($carts as $cart){
                    $products = $this->mproduct->get_productbyid($cart->product_id);
                    if($products[0]['new_price']){
                        $amount = $cart->quantity*$products[0]['new_price'];
                    }
                    else{
                        $amount = $cart->quantity*$products[0]['unit_price'];
                    }
                    $detail = array(
                        'invoice_id' => $id,
                        'product_id' => $products[0]['product_id'],
                        'quantity' => $cart->quantity,
                        'amount' => $amount
                    );
                    $this->minvoicedetail->create($detail);
                    $this->mcart->remove($uid, $products[0]['product_id']);
                }
                redirect("frontend/purchase/cart");
            }
        }
        $this->load->view('/frontend/cart',$data);
    }

    public function add_cart(){
        $pid = $this -> input -> post('pid');
        $quantity = $this -> input->post('quantity');
        $uid = $this->session->userdata('logged_in')['uid'];
        $products = $this->mproduct->get_productbyid($pid);

        if($quantity > $products[0]['quantity_remaining']){
            echo 2;
        }
        else{
            if ($quantity==0) $this->mcart->remove($uid,$pid);
            else $this->mcart->add($uid,$pid,$quantity);
            echo 1;
        }
    }
    public function delete_cart(){
        $pid = $this->input->post('pid');
        $uid = $this->session->userdata('logged_in')['uid'];
        $this->mcart->remove($uid, $pid);
    }
}